package com.quantumoctopus.magiceatball;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class LogActivity extends Activity implements OnItemClickListener {

	private ArrayList<String> names = new ArrayList<String>();
	private ArrayList<String> ids = new ArrayList<String>();
	int position;
	private LogArrayAdapter logAdapter;
	private ListView listView;
	public static final String PREFS_EATBALL = "EatBallPrefsFile";
	static SharedPreferences settings;
	SharedPreferences.Editor editor;
	private SensorManager mSensorManager;
	private ShakeEventListener mSensorListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.log_activity_layout);
		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(this);

		// This is just using a test string, will modify for final log string
		// format

		// SharedPreferences settings = getPreferences(MODE_PRIVATE);
		createList();

		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mSensorListener = new ShakeEventListener();
		mSensorListener
				.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

					@Override
					public void onShake() {
						clearLog();
					}
				});

	}

	private void createList() {
		settings = getSharedPreferences(PREFS_EATBALL, 0);
		String logstring = settings.getString("savedlogslist", ""); // test
		String[] pairs = logstring.split(";;");// getResources().getString(R.string.testLog).split(";;");
		for (int i = 0; i < pairs.length; i++) {
			String[] nameIdPair = pairs[i].split("::");
			names.add(nameIdPair[0]);
			if (nameIdPair.length > 1)
				ids.add(nameIdPair[1]);
		}

		logAdapter = new LogArrayAdapter(this, names);
		listView.setAdapter(logAdapter);
	}

	public void clearLog() {

		settings = getSharedPreferences(PREFS_EATBALL, 0);
		editor = settings.edit();
		editor.putString("savedlogslist", "");
		editor.commit();
		
		logAdapter.clear();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View row, int position,
			long id) {
		this.position = position;
		if (!names.get(position).equals(""))
			new GetResultFromLogTask().execute();

	}

	public class GetResultFromLogTask extends AsyncTask<Integer, Integer, Long> {

		@Override
		protected Long doInBackground(Integer... params) {
			Result r = ResultsWorker.getResultByID(ids.get(position));

			Intent intent = new Intent(LogActivity.this, ResultActivity.class);
			intent.putExtra("name", r.getName());
			intent.putExtra("display_address", r.getDisplay_address());
			intent.putExtra("phone", r.getPhone());
			intent.putExtra("url", r.getUrl());
			intent.putExtra("id", r.getId());
			intent.putExtra("rating", Float.parseFloat(r.getRating()));
			startActivity(intent);
			return null;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();

		// enable shake listener
		mSensorManager.registerListener(mSensorListener,
				mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_UI);
	}

	@Override
	protected void onPause() {
		mSensorManager.unregisterListener(mSensorListener);
		super.onPause();
	}
}
